package com.tao.com.xiaowen.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.tao.com.xiaowen.model.News;
import com.tao.com.xiaowen.R;

import java.util.List;

/**
 * Created by Administrator on 2016/8/17.
 */
public class NewsFragmentAdapter extends XRecyclerView.Adapter<NewsFragmentAdapter.NewsViewHolder> {
    private Context context;
    private LayoutInflater inflater;
    private List<News> newsList;

    private OnItemClickListener onItemClickListener;

    public NewsFragmentAdapter(Context context, List<News> newsList) {
        this.context = context;
        this.newsList = newsList;
        inflater = LayoutInflater.from(context);
    }

    public void OnItemClick(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public NewsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.fragment_news_item, null);
        NewsViewHolder holder = new NewsViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(NewsViewHolder holder, final int position) {
        News news = newsList.get(position);

//        Date date = new Date(Long.parseLong(news.getBehot_time()));
//        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
//        String time = sf.format(date);
        String time = news.getDate();

        holder.titleView.setText(news.getTitle());
        holder.sourceView.setText(news.getAuthor_name());

        Glide.with(context).load(news.getThumbnail_pic_s())
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .placeholder(R.drawable.default_content_pic)
                .error(R.drawable.default_content_pic)
                .crossFade()
                .into(holder.picView);

        holder.timeView.setText(time);

        if (onItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onItemClickListener.OnItemClick(view, position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return newsList.size();
    }

    class NewsViewHolder extends XRecyclerView.ViewHolder {
        private TextView titleView;
        private TextView sourceView;
        private TextView timeView;
        private ImageView picView;

        public NewsViewHolder(View itemView) {
            super(itemView);
            titleView = (TextView) itemView.findViewById(R.id.news_title);
            sourceView = (TextView) itemView.findViewById(R.id.news_source);
            timeView = (TextView) itemView.findViewById(R.id.news_time);
            picView = (ImageView) itemView.findViewById(R.id.news_pic);
        }
    }

    public interface OnItemClickListener {
        void OnItemClick(View view, int position);
    }
}
